package com.ruoyi.jihuo.service.handler;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.jihuo.api.CallTaoApiService;
import com.ruoyi.jihuo.domain.JhTmAccount;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author：LXY
 * @DATE：2025-09-14 15:12
 * @Description：获取商品发布规则
 */
@Slf4j
@Component
public class Jh70GetSubmitItemSchemaHandler implements PriorityHandler {
    @Autowired
    private CallTaoApiService callTaoApiService;
    @Override
    public int getPriority() {
        return 70;
    }

    @Override
    public boolean handle(ProcessContext<JSONObject> request) throws IOException {
        JSONObject data = request.getData();
        Long productId = data.getLong("productId");
        Long cid = data.getLong("categoryId");
        JhTmAccount jhTmAccount = data.getObject("tmAccount", JhTmAccount.class);
        try {
            String schema = callTaoApiService.getSubmitItemSchema(jhTmAccount, cid, productId);
            data.put("submitItemSchema", schema);
        }catch (Exception e){
            request.setErrorInfo("获取商品发布规则异常:" + e.getMessage());
            throw new RuntimeException("获取商品发布规则异常:" + e.getMessage());
        }
        return true;
    }
}
